
cc.Class({
    extends: cc.Component,

    properties: {
     //爆炸特效
     Bang:[cc.Prefab],  
     
     //敌机子弹
     enemy_bullet:[cc.Prefab],
    },

   

   onLoad () {
    this.isdied = false;
    
   },

   init:function(type){
  
    //设置敌机编号
    this.node.type = type;  
    //设置不同敌机的血量 
    if(type == 0){
        this.HP = 1000;
        this.node.hp = 1000;
              
    }
    if(type == 1){
        this.HP = 500;
        this.node.hp = 500;
    }
    if(type == 2){
        this.HP = 500;
        this.node.hp = 500; 
    }
    this.speed = 1.5;
   
    this.node.y = cc.winSize.height / 2 + this.node.height / 2;
    var randomx = window.Global.getRandom(-cc.winSize.width / 2 + this.node.width / 2,cc.winSize.width / 2 - this.node.width / 2);
    this.node.x = randomx;
    if(type != 2){
        //生成敌机子弹
        this.schedule(this.createEnemy_bullet,3); 
        this.score = window.Global.enemyData[this.node.type];
    }else{
        this.score = window.Global.enemyData[this.node.type];   
    }

   },
   
   //敌机子弹生成
   createEnemy_bullet:function(){
    
    this.enmey_bull = window.Global.getClassAndAdd(this.enemy_bullet[this.node.type],"Enemy_bullet",this.node.parent); 
    this.enmey_bull.tClass.init(this.node.type,0);  
    this.enmey_bull.tNode.setPosition(this.node.position);
   },


   //子弹与敌机碰撞检测
    onCollisionEnter:function(other,self){
        if(this.isdied){
            return;
        }
        if(other.node.group == "bullet"){
         
            if(self.node.hp <= 0){   
                this.isdied = true;
                self.node.children[0].destroy();
                self.node.getComponent(cc.Animation).play();
        
            }else{
        
                self.node.children[0].active = true;
                this.node.hp -= 100;
                self.node.children[0].getComponent(cc.ProgressBar).progress = this.node.hp / this.HP;
        
            }
        
            let bang = window.Global.getClassAndAdd(this.Bang[self.node.type],"Bang",self.node.parent); 
            bang.tNode.setPosition(other.node.position);
            other.node.destroy();    
        }
      
    },

    die:function(){
    
    window.Global.score +=  this.score;
    window.Global.Stage_score += this.score;
    this.node.destroy();
    },

    update (dt) {

    this.node.y -= this.speed;
    
    if(this.node.type == 2 && this.node.y <= 0){
        let rand = parseInt(Math.random()*35);
        for(let i = 0; i < 36; i++){
            if(i == rand || i == rand + 1){
                continue;
            }
            var enmey_bullet = window.Global.getClassAndAdd(this.enemy_bullet[this.node.type],"Enemy_bullet",this.node.parent); 
            
            this.rotation = 10 * i;
                 
            enmey_bullet.tClass.init(4,this.rotation);  
            enmey_bullet.tNode.setPosition(this.node.position);
         }
        this.node.destroy();
    }

    if(this.node.y < -cc.winSize / 2 - this.node.height / 2){
        window.Global.destroyNode(this.node);  
    }
    
    },
});
